package org.molamil.acto.core.parsers {
	import flash.utils.getQualifiedClassName;	
	
	import org.molamil.acto.core.loggers.Logger;	
	import org.molamil.acto.core.parsers.PrioritizableList;	
	import org.molamil.acto.core.parsers.ParserError;	
	
	/**
	 * XXX: Document.
	 * 
	 * @author Jorge Hernandez
	 */
	public class ParserResolver extends PrioritizableList implements Parser {

		
		// CONSTANTS
		
		private static const logger:Logger = Logger.getLogger("ParserResolver");
		
		
		// PUBLIC METHODS
		
		public function parse(value:String):* {
			
			logger.debug("parse(), value = " + value);
			
			sort();
			
			for (var i:Number = 0; i < _a.length; i++) {
				var parser:Parser = _a[i];
				try {
					var o:* = parser.parse(value);
					logger.debug("value parsed to " + o);
					logger.debug("value parsed by " + getQualifiedClassName(parser));
					return o;
				} catch (e:ParserError) {
					continue;
				}
			}
			
			throw new ParserError("No Parser found to resolve '" + value + "'");
			
		}
		
		
	}
	
}
